﻿#ifndef __SS_ASIO_THREAD_H__
#define __SS_ASIO_THREAD_H__
#include "ssAsioSession.h"
#include <map>
#include <functional>

namespace StarSeeker
{

class ssAsioService : public asio::io_service
{
public:
	ssAsioService() :m_ioservice(1)
	{
	}
	ssAsioService(int concurrency_hint) :m_ioservice(concurrency_hint)
	{
	}
	void Run()
	{
		m_ioservice.run();
	}

	asio::io_service	m_ioservice;
};

class ssAsioThread
{
	friend class ssAsioNetWork;
public:
	ssAsioThread();
	~ssAsioThread();
	//运行事件服务
	void RunService();
	//返回连接个数
	u32 GetSessionCount();
	//释放所有连接
	void ClearSession();
	//设置线程名
	void SetThreadName(u32 index, const char* name, const wchar_t* wname);
private:
	//查找session
	ssAsioSessionPtr FindSession(u32 id);
	//设置线程名
	void HandleSetThreadName(const asio::error_code& error, SS_SHARED_PTR<asio::steady_timer>& timer, const std::string& name, const std::wstring& wname);
public:
	asio::io_service					m_service;
	asio::executor_work_guard<asio::io_context::executor_type>	m_work;
	std::thread							m_thread;
	lint								m_sendEventCount;	//未处理完的发送事件次数
	lint								m_sendQueueCount;	//发送队列数
private:
	std::map<u32, ssAsioSessionPtr>		m_sessionMap;		//连接对象
};
typedef SS_SHARED_PTR<ssAsioThread> bThreadPtr;
typedef SS_SHARED_PTR<asio::ip::tcp::acceptor> bAcceptorPtr;

}
#endif

